package com.sanji.pay.allinpay.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.TreeMap;

@Component
public class SybPayService {
  @Value("${allinpay.cusid}")
  public String SYB_CUSID ;
  @Value("${allinpay.appid}")
  public String SYB_APPID ;
  @Value("${allinpay.appkey}")
  public String SYB_APPKEY ;
  @Value("${allinpay.apiurl}")
  public String SYB_APIURL;//生产环境
  /**
   *
   * @param trxamt 交易金额
   * @param reqsn 商户交易单号
   * @param paytype 交易方式
                    W01：微信扫码支付
                    A01：支付宝扫码支付
                    Q01：手机QQ扫码支付
   * @param body 订单标题
   * @param remark 备注
   * @param acct 有效时间
   * @param authcode
   * @param notify_url 交易结果通知地址
   * @param limit_pay 支付限制
   * @return
   * @throws Exception
   */
	public Map<String,String> pay(long trxamt,String reqsn,String paytype,String body,String remark,String acct,String authcode,String notify_url,String limit_pay) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SYB_APIURL+"/pay");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", SYB_CUSID);
		params.put("appid", SYB_APPID);
		params.put("version", "11");
		params.put("trxamt", String.valueOf(trxamt));
		params.put("reqsn", reqsn);
		params.put("paytype", paytype);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("body", body);
		params.put("remark", remark);
		params.put("notify_url", notify_url);
		params.put("validtime", "1");//二维码过期时间
		params.put("limit_pay", limit_pay);
		params.put("sign", SybUtil.sign(params,SYB_APPKEY));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
    /**
     * cusid 商户号 appid 应用ID trxid 交易单号 reqsn 商户订单号
     * trxstatus 交易状态 fintime 交易完成时间 errmsg 错误原因
     * randomstr 随机字符串 sign 签名
     */
		Map<String,String> map = handleResult(result);
		return map;
		
	}

  /**
   * 撤销当天支付
   * @param trxamt 原订单金额
   * @param reqsn 商户的退款交易订单号
   * @param oldtrxid 原交易的商户交易单号
   * @param oldreqsn 原交易的收银宝平台流水
   * @return
   * @throws Exception
   */
	public Map<String,String> cancel(long trxamt,String reqsn,String oldtrxid,String oldreqsn) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SYB_APIURL+"/cancel");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", SYB_CUSID);
		params.put("appid", SYB_APPID);
		params.put("version", "11");
		params.put("trxamt", String.valueOf(trxamt));//金额
		params.put("reqsn", reqsn);//商户的退款交易订单号
		params.put("oldtrxid", oldtrxid);//原商户交易单号
		params.put("oldreqsn", oldreqsn);//原收银宝平台流水
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("sign", SybUtil.sign(params,SYB_APPKEY));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}

  /**
   * 退款
   * @param trxamt
   * @param reqsn
   * @param oldtrxid
   * @param oldreqsn
   * @return
   * @throws Exception
   */
	public Map<String,String> refund(long trxamt,String reqsn,String oldtrxid,String oldreqsn) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SYB_APIURL+"/refund");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", SYB_CUSID);
		params.put("appid", SYB_APPID);
		params.put("version", "11");
		params.put("trxamt", String.valueOf(trxamt));
		params.put("reqsn", reqsn);
		params.put("oldreqsn", oldreqsn);
		params.put("oldtrxid", oldtrxid);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("sign", SybUtil.sign(params,SYB_APPKEY));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}

  /**
   * 交易查询
   * @param reqsn 商户的交易订单号
   * @param trxid 支付的收银宝4平台流水
   * @return
   * @throws Exception
   */
	public Map<String,String> query(String reqsn,String trxid) throws Exception{
		HttpConnectionUtil http = new HttpConnectionUtil(SYB_APIURL+"/query");
		http.init();
		TreeMap<String,String> params = new TreeMap<String,String>();
		params.put("cusid", SYB_CUSID);
		params.put("appid", SYB_APPID);
		params.put("version", "11");
		params.put("reqsn", reqsn);
		params.put("trxid", trxid);
		params.put("randomstr", SybUtil.getValidatecode(8));
		params.put("sign", SybUtil.sign(params,SYB_APPKEY));
		byte[] bys = http.postParams(params, true);
		String result = new String(bys,"UTF-8");
		Map<String,String> map = handleResult(result);
		return map;
	}
	
	
	public Map<String,String> handleResult(String result) throws Exception{
		Map map = SybUtil.json2Obj(result, Map.class);
		if(map == null){
			throw new Exception("返回数据错误");
		}
		if("SUCCESS".equals(map.get("retcode"))){
			TreeMap tmap = new TreeMap();
			tmap.putAll(map);
			String sign = tmap.remove("sign").toString();
			String sign1 = SybUtil.sign(tmap,SYB_APPKEY);
			if(sign1.toLowerCase().equals(sign.toLowerCase())){
				return map;
			}else{
				throw new Exception("验证签名失败");
			}
			
		}else{
			throw new Exception(map.get("retmsg").toString());
		}
	}
	
	
}
